<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我要发布</title>
    <link type="text/css" rel="stylesheet" href="./layui/css/layui.css"/>
</head>
<body class="layui-layout-body">
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>我要发布</legend>
</fieldset>
<div class="layui-body" style="left: 13px;margin-top: 68px;">
    <!-- 内容主体区域 -->
    <script type="text/javascript" src="layui/layui.all.js"></script>
    <div style="padding: 20px;">发布文章</div>
    <form class="layui-form" action="">

        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input  title"
                       type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label state">标记</label>
            <div class="layui-input-block" style="padding-top: 13px">
                <input name="close" lay-skin="switch" lay-text="ON|OFF" type="checkbox" class="state">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" class="layui-textarea" rows="20" cols="20"></textarea>
            </div>
        </div>

        <!--<div class="layui-form-item layui-form-text">
          <label class="layui-form-label">编辑器</label>
          <div class="layui-input-block">
            <textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="LAY_demo_editor"></textarea>
          </div>
        </div>-->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="button" class="layui-btn submit" value="发布">
                <input type="button" class="layui-btn save" value="保存至草稿箱">
            </div>
        </div>
    </form>
</div>

<script src="./layui/layui.all.js"></script>
<script type="text/javascript" src="./jquery-1.9.1.js"></script>
<script>
    //    var $ = layui.jquery;
    $(".submit").click(function () {
        console.log(".....")
        var $title = $(".title").val();
        var $tag = $(".state").serialize();
        if ($tag === "") {
            $tag = 0;
        } else {
            $tag = 1;
        }
        var $areatext = $(".layui-textarea").val();
        var publishdate = new Date();
        var year = publishdate.getFullYear();
        var month = publishdate.getMonth() + 1;
        var date = publishdate.getDate();
        var hour = (publishdate.getHours() < 10) ? ("0" + publishdate.getHours()) : publishdate.getHours();
        var min = (publishdate.getMinutes() < 10) ? ("0" + publishdate.getMinutes()) : publishdate.getMinutes();
        var sec = (publishdate.getSeconds() < 10) ? ("0" + publishdate.getSeconds()) : publishdate.getSeconds();
        var now = year + "年" + month + "月" + date + "日" + hour + ":" + min + ":" + sec;
        var formdata = "title=" + $title + "&tag=" + $tag + "&content=" + $areatext + "&publishdate=" + now + "&state=" + 1;
        $.ajax({
            url: "/publishcontent.do",
            type: "Post",
            data: formdata
        }).done(function (data) {
            console.log(data.msg)
            if (data.msg === "发布成功") {
                window.alert("发布成功");
            } else {
                window.alert("发布失败");
            }
        }).fail()
    });
    $(".save").click(function () {
        var $title = $(".title").val();
        var $tag = $(".state").serialize();
        if ($tag === "") {
            $tag = 0;
        } else {
            $tag = 1;
        }
        var $areatext = $(".layui-textarea").val();
        var publishdate = new Date();
        var year = publishdate.getFullYear();
        var month = publishdate.getMonth() + 1;
        var date = publishdate.getDate();
        var hour = (publishdate.getHours() < 10) ? ("0" + publishdate.getHours()) : publishdate.getHours();
        var min = (publishdate.getMinutes() < 10) ? ("0" + publishdate.getMinutes()) : publishdate.getMinutes();
        var sec = (publishdate.getSeconds() < 10) ? ("0" + publishdate.getSeconds()) : publishdate.getSeconds();
        var now = year + "年" + month + "月" + date + "日" + hour + ":" + min + ":" + sec;
        var formdata = "title=" + $title + "&tag=" + $tag + "&content=" + $areatext + "&publishdate=" + now + "&state=" + 0;
        $.ajax({
            url: "/publishcontent.do",
            type: "Post",
            data: formdata
        }).done(function (data) {
            if (data.msg === "发布成功") {
                window.alert("已存储到草稿箱!");
            } else {
                window.alert("存储失败!!");
            }
        }).fail()
    })
</script>
</body>
</html>